package com.yy.dao.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.yy.model.TbJob;
import com.yy.dao.IJobDAO;;
@Component
public class JobDAO implements IJobDAO{
	@Resource
	private SessionFactory sessionFactory;
	
	@SuppressWarnings("unchecked")
	@Override
	public List<TbJob> getAllJob(Integer jobcompany) throws Exception{
		String hql="select tbjob from TbJob as tbjob where jobCompany="+jobcompany+"";
		System.out.println("it is Job DAO ************");
		Query query=this.sessionFactory.getCurrentSession().createQuery(hql);
		return query.list();
	}
	@SuppressWarnings("unchecked")
	@Override
	public int getJobCount(Integer jobcompany) throws Exception {
		// TODO Auto-generated method stub
		int result = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "select count(*) from TbJob where jobCompany="+jobcompany+"  ";
			Query query = session.createQuery(hql);
			result = Integer.parseInt(query.uniqueResult().toString());
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public String getJobName(int jobId) throws Exception{
		String hql = "SELECT jobName FROM TbJob WHERE jobId="+jobId;
		Query query = this.sessionFactory.getCurrentSession().createQuery(hql);
		return query.uniqueResult().toString();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int changeJob(int jobId, String jobname, int jobnum,
			int jobhave) throws Exception {
		// TODO Auto-generated method stub
		int result=0;
		try{
			Session session=sessionFactory.getCurrentSession();
			String sql="update TbJob set jobName='"+jobname+"', jobNum='"+jobnum+"', jobHave='"+jobhave+"' where jobId="+jobId+"  ";
			Query query=session.createQuery(sql);
			query.executeUpdate();
			System.out.println(sql);
			result=10;
		}catch(Exception e){
			e.printStackTrace();
			//result=0;
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int delJob(int jobId) throws Exception {
		// TODO Auto-generated method stub
		int result=0;
		try{
			Session session=sessionFactory.getCurrentSession();
			String hql="delete from TbJob where jobId="+jobId+" ";
			Query query=session.createQuery(hql);
			query.executeUpdate();
			result=10;
			
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return result;
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int addJob(TbJob tbjob) throws Exception {
		// TODO Auto-generated method stub
		int result=0;
		try{
			Session session=sessionFactory.getCurrentSession();
			result = (int) session.save(tbjob);
			System.out.println("add Job finish **************");
			result=10;
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return result;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public int getCompanyId(int jobId) throws Exception{
		int result = 0;
		try {
			Session session = sessionFactory.getCurrentSession();
			String hql = "select jobCompany from TbJob  where jobId="+jobId+"";
			Query query = session.createQuery(hql);
			result = Integer.parseInt(query.uniqueResult().toString());
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		System.out.println("hhahahahah+"+result);
		return result;
	}

}
